const template = `

<div class="sale-wraper">
    <i>img</i>
    <em>{{data.name}}</em>
    <span class="price">{{data.price}}元</span>
    
    <a class="reduce" v-show="data.num!=0" @click="changeNum(data.num-1)" ></a>
    <span class="num" v-show="data.num!=0">{{data.num}}</span>
    <a class="plus" @click="changeNum(data.num+1)"></a>
</div>

`

export default {
    template,
    props: {
        data: {
            type: Object,
            default: {}
        },
        order: {
            type: Object,
            default: {}
        }
    },
    data() {
        return {
            num: 0
        }
    },
    methods: {
        changeNum(newNum) {
            if (newNum < this.data.num) {
                this.order.num -= 1;
                this.order.price -= this.data.price;
                var data = this.data
                var flag = this.order.list.find((n) => n.id === data.id);
                flag.num-=1;
                this.$emit("input", this.order);

            } else {
                var data = this.data
                var flag = this.order.list.find((n) => n.id === data.id);
                if(flag){
                    flag.num+=1;
                }else{
                    this.order.list.push(this.data);
                }
                this.order.num += 1;
                this.order.price += this.data.price;
                this.$emit("input", this.order);
            }
            this.data.num = newNum;
        }
    }
}